const Monitor = require('../../utils/monitor.js').Monitor
const util = require('../../utils/util.js')
Page(Monitor.hookApp({
  data: {
    hand_settlement: -1, // 让不让点击提现规则
    kongImage: false,
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    navList: [{
        src: getApp().globalData.imgUrl + 'jsy-newshop分销订单.svg',
        title: '粉丝订单'
      },
      {
        src: getApp().globalData.imgUrl + 'jsy-newshop我的选货.svg',
        title: '我选的货'
      },
      {
        src: getApp().globalData.imgUrl + 'jsy-newshop收益明细.svg',
        title: '收益明细'
      },
      {
        src: getApp().globalData.imgUrl + 'jsy-newshop提现记录.svg',
        title: '提现记录'
      }
    ],
    fenxiaoNav: [{
        title: '我的粉丝',
        prise: 0
      },
      {
        title: '分销商品',
        prise: 0
      },
      {
        title: '提成',
        prise: 0
      }
    ],
    authorization: true,
    showPhoto: false,
    permissionSetting: {
      setingOn: false,
      save: true,
      typeShare: 2
    }
  },
  onShareAppMessage(e) {
    var shareimage = getApp().globalData.shareimage;
    shareimage ? shareimage = shareimage.tempFilePath : "";
    if (this.data.permissionSetting.data) {
      var goods_id = this.data.permissionSetting.data[0].goods_id;
    } else {
      var goods_id = this.data.list.distributeGoods[0].goods_id
    }
    console.log(goods_id)
    return {
      title: getApp().globalData.shopname,
      path: 'pages/index/index?scene=normal' + `&member_id=${getApp().globalData.member_id}&type=2&goods_id=${goods_id}`,
      imageUrl: shareimage,
      success: (res) => { // 成功后要做的事情   
        wx.showToast({
          title: '转发成功',
        })
      },
      fail: function(res) {
        wx.showToast({
          title: '转发失败',
        })
      }
    }
  },
  getUserInfo: function(e) {
    var filterArr = this.data.list.distributeGoods.filter((item, index) => index == e.currentTarget.dataset.index);
    if (e.detail.userInfo) {
      this.setData({
        authorization: e.detail.userInfo,
        showPhoto: true,
        'permissionSetting.data': filterArr
      })
    }

  },
  // myevent(e) {
  //   var that = this
  //   that.setData({
  //     permissionSetting: e.detail.permissionSetting
  //   })
  // },
  jump(e) { //第二行跳转
    var e = e.currentTarget.dataset.idx
    if (e == 0) {
      wx.navigateTo({
        url: '../fxOrder/fxOrder?status=99',
      })
    } else if (e == 1) {
      wx.navigateTo({
        url: '../WoXuanDeHuo/WoXuanDeHuo',
      })
    } else if (e == 2) {
      wx.navigateTo({
        url: '../ShouYiMingXi/ShouYiMingXi?index=1',
      })
    } else if (e == 3) {
      wx.navigateTo({
        url: '../TiXianJiLu/TiXianJiLu',
      })
    }
  },
  jumpCash(e) { // 点击跳转提现
    if (this.data.hand_settlement == 1) { // 可以点击提现规则
      let ev = e.currentTarget.dataset
      wx.navigateTo({
        url: `../TiXian/TiXian`,
        // url: `../TiXian/TiXian?distribute_lv_id=${ev.distribute_lv_id}&distribute_type=${ev.distribute_type}&store_id=${ev.store_id}&cashmoney=${ev.cashmoney}&return=${JSON.stringify(this.data.return)}`,
      })
    } else {
      wx.showToast({
        title: '暂不支持手动提现',
        icon: 'none'
      })
    }
  },
  jumpRules() { // 点击跳转提现规则
    if (this.data.hand_settlement == 1) { // 可以点击提现规则
      wx.navigateTo({
        url: '../Rouls/Rouls',
      })
    } else {
      wx.showToast({
        title: '暂不支持手动提现',
        icon: 'none'
      })
    }

  },
  jumpTop(e) { //跳转第一行
    var e = e.currentTarget.dataset.index
    if (e == 0) {
      wx.navigateTo({
        url: '../FenXiaoXiaJi/FenXiaoXiaJi',
      })
    } else if (e == 1) {
      wx.navigateTo({
        url: '../fxShopList/fxShopList',
      })
    } else if (e == 2) {
      wx.navigateTo({
        url: '../ShouYiMingXi/ShouYiMingXi?index=' + e,
      })
    } else if (e == 3) {
      wx.navigateTo({
        url: '../ShouYiMingXi/ShouYiMingXi?index=' + e,
      })
    }
  },
  getData() {
    var that = this
    util.API('spwx/v1.Shop/distributionCenter', {}).then(res => {
      let {
        code,
        data,
        msg
      } = res
      if (code === 200) {
        this.data.fenxiaoNav[0].prise = data.subordinate
        this.data.fenxiaoNav[1].prise = data.goodsCount
        this.data.fenxiaoNav[2].prise = data.total_cashmoney ? data.total_cashmoney : 0, // ?????
          // data.distributeGoods = data.distributeGoods.map(item=>{
          //   item.goods_cover = item.goods_cover.split(',')[0]
          //   return item
          // })

          this.setData({
            list: data,
            fenxiaoNav: this.data.fenxiaoNav,
          })

        getApp().authorization().then(res => {
          that.setData({
            authorization: res,
          })
        })
        this.getRules()
      } else {
        this.setData({
          kongImage: true
        })
        wx.showToast({
          title: msg + '',
          icon: 'none',
          success() {
            if (msg == "没有分销员权限!") {
              setTimeout(() => {
                wx.redirectTo({
                  url: '/pages/FenXiaoYuanShenQing/FenXiaoYuanShenQing',
                })
              }, 1500)
            }
          }
        })
      }
    })
  },
  getRules() {
    util.API('spwx/v1.Distribut/showDistributeSetting').then(res => {
      let {
        code,
        data,
        msg
      } = res
      if (code == 200) {
        this.setData({
          hand_settlement: data.return.hand_settlement,
          member: data.member,
          return: data.return
        })
      }
    })
  },
  addRecommend(e) {
    // util.API('pyapi/add_my_chose_goods', {
    //   goods_id: e.currentTarget.dataset.goods_id
    // }, 'application/x-www-form-urlencoded').then(res => {
    //   let {
    //     code,
    //     msg
    //   } = res
    //   wx.showToast({
    //     title: msg + '',
    //     icon: 'none'
    //   })
    // })
  },



  onLoad: function(options) {

  },
  onShow() {
    this.getData()
  }
}))